<template>
  <el-card>
    <el-table
      :data="menuList"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="code" label="权限值" />
      <el-table-column label="操作">
        <template #="{ row, $index }">
          <el-button
            type="primary"
            size="small"
            icon="Plus"
            :disabled="row.level == 4 ? true : false"
            @click="saveMenu(row)"
          >
            {{ row.level == 3 ? '添加按钮' : '添加菜单' }}
          </el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            :disabled="row.level == 1 ? true : false"
            @click="updateMenu(row)"
          >
            编辑
          </el-button>
          <el-popconfirm
            :title="`您确定删除${row.name}吗？`"
            @confirm="remove(row.id)"
            width="200px"
          >
            <template #reference>
              <el-button
                type="primary"
                size="small"
                icon="Delete"
                :disabled="row.level == 1 ? true : false"
              >
                删除
              </el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <el-dialog
    v-model="dialogVisible"
    :title="menuForm.id ? '更新' : '添加'"
    width="30%"
  >
    <el-form label-width="80px">
      <el-form-item label="名称">
        <el-input v-model="menuForm.name"></el-input>
      </el-form-item>
      <el-form-item label="权限值">
        <el-input v-model="menuForm.code"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { reqSaveOrUpdate, reqRemove, reqMenuList } from '@/api/auth/menu'

let dialogVisible = ref(false)

let menuList = ref([])
let menuForm = reactive({
  id: 0,
  level: 0,
  name: '',
  code: '',
  pid: 0,
})

//初始化
onMounted(() => {
  getMenuList()
})
//获取菜单列表
const getMenuList = async () => {
  let res = await reqMenuList()
  if (res.code == 200) {
    menuList.value = res.data
  }
}
//保存菜单按钮
const saveMenu = (row) => {
  dialogVisible.value = true
  Object.assign(menuForm, {
    id: 0,
    level: 0,
    name: '',
    code: '',
    pid: 0,
  })
  menuForm.pid = row.id
  menuForm.level = row.level + 1
}
//更新菜单按钮
const updateMenu = (row) => {
  dialogVisible.value = true
  Object.assign(menuForm, row)
}
//保存业务按钮
const save = async () => {
  let res = await reqSaveOrUpdate(menuForm)
  if (res.code == 200) {
    if (menuForm.id) {
      ElMessage.success('更新菜单成功')
    } else {
      ElMessage.success('添加菜单成功')
    }
    getMenuList()
  } else {
    ElMessage.error('操作失败')
  }
  dialogVisible.value = false
}
//删除
const remove = async (id) => {
  let res = await reqRemove(id)
  if (res.code == 200) {
    ElMessage.success('删除成功')
  } else {
    ElMessage.error('操作失败')
  }
  getMenuList()
}
</script>
<style scoped lang="scss"></style>
